import os
from typing import TYPE_CHECKING
from PySide6.QtWidgets import QListWidgetItem
from gui.dialogbox import FileMenuDialog, SetWindow
from filetools import Check, ConfigLoader
if TYPE_CHECKING:
    from gui.window import Window



CONFIG = ConfigLoader().Config() # 全局配置加载


class IndependentWindow:
    """
    类负责统一管理依赖的非主要对话框和第二窗口和展示框的导入依赖支持
    """
    @staticmethod
    def filedialog(*args):
        return FileMenuDialog(*args)

    @staticmethod
    def setdialog(*args):
        return SetWindow(*args)

class UiLogic:
    """
    本类实现所有的ui全部的基础交互逻辑，和部分ui的高级逻辑
    """
    def __init__(self, master = None):
        self.window:Window|None = master
        self.dialog = IndependentWindow()
        self.allfile:dict[str,tuple[str,str]] = {} # 总文件数据集
        self.filenamelist = [] # 预备执行文件列表

    def input_data(self, text:str,):
        """
        方法实现针对主窗口的日志展示框的插入参数工作
        :param text: 已经出来完成需要插入的参数
        :param color: 设置文字显示的颜色
        :return: 返回列表展示框对象
        """

    def menu_open_file(self):
        """
        此方法用于接受主窗口 main_window 类的文件菜单的打开动作回调
        """
        filepath = self.dialog.filedialog().file_dialog()
        checkdata = Check().CheckEntrance(filepath)
        # 日志节点
        filelist = []
        for tag, name, insidepath in checkdata:
            for path in filepath:
                if name in path and tag == 0:
                    insidepath = os.path.normpath(insidepath)
                    self.allfile[name] = path, insidepath
            data = name, tag
            filelist.append(data)
        # 日志节点
        self.window.createitem(filelist)
        # 日志节点

    def menu_save_file(self):
        filedialog = self.dialog.filedialog()
        save_file = filedialog.save_file_dialog()
        return save_file

    def menu_saveas_file(self):
        filedialog = self.dialog.filedialog()
        savaas_file = filedialog.saveas_file_dialog()
        return savaas_file

    def menu_senior_setting(self):
        setdialog = self.dialog.setdialog(SetWindowLogic)
        win = setdialog.open_win()
        return win

    def menu_senior_more(self):
        pass
    def menu_help_about(self):
        pass
    def menu_help_thank(self):
        pass
    def clicked_button_check_all(self):
        pass
    def clicked_button_clean(self):
        self.window.listwidget.clear()

    def clicked_button_refresh(self):
        pass
    def clicked_button_search(self):
        self.search_data = self.window.input_search.text()

    def clicked_button_api_save(self):
        pass

    def clicked_button_api_verify(self):
        pass

    def clicked_button_start(self, path:str):
        pass

    def clicked_button_stop(self):
        pass

    def clicked_button_pause(self):
        pass

    def clicked_button_pageup(self):
        scrollbar = self.window.show_box_scrollbar
        scrollbar.triggerAction(scrollbar.SliderAction.SliderPageStepSub)
        return scrollbar

    def clicked_button_pagedown(self):
        scrollbar = self.window.show_box_scrollbar
        scrollbar.triggerAction(scrollbar.SliderAction.SliderPageStepAdd)
        return scrollbar

    def clicked_radio_api(self, item = None):
        if item:
            self.window.input_apikey.setPlaceholderText(f'请输入{item}翻译的apikey')
            self.window.input_appid.setPlaceholderText(f'请输入{item}翻译的appid')
            return item
        else: return '请选择api模式'
    
    def itemClicked_listwidget_item(self,item:QListWidgetItem):
        uncheck = self.window.Qt.CheckState.Unchecked
        check = self.window.Qt.CheckState.Checked
        state = item.checkState()
        if state is uncheck:
            item.setCheckState(check)
            name = item.text()
            if name in self.allfile:
                allfile = self.allfile[name]
                path1 = allfile[0]
                path2 = allfile[1]
                path = os.path.join(path1,path2)
                self.filenamelist.append(path)
        else:
            item.setCheckState(uncheck)
        

class SetWindowLogic:
    """
    本类实现针对设置对话框的全部基础，高级ui逻辑实现
    """
    def __init__(self, master = None):
        self.setdialog = master

def test():
    a = UiLogic()
    a.input_data('1')

if __name__ == '__main__':
    test()